package com.ysd.lis.socket;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Date;

@EnableScheduling
@Component
public class WebSocketSchedule {
    @Autowired
    @Scheduled(cron = "* * * * * ?")
    public void heartbeat() {
        //每天的23点50到0点10分推送数据
        if (DateUtil.compare(new DateTime(), DateUtil.parse(DateUtil.format(new Date(), "yyyy-MM-dd") + " 23:50:00")) > 0
                || DateUtil.compare(new DateTime(), DateUtil.parse(DateUtil.format(new Date(), "yyyy-MM-dd") + " 00:10:00")) < 0
        ) {
            MessageTemplate messageTemplate = new MessageTemplate();
            messageTemplate.setType("1");
            messageTemplate.setMessage(DateUtil.format(LocalDateTime.now(), "yyyy-MM-dd HH:mm:ss"));
            WebSocketUtils.sendAllMessage(messageTemplate);
        }
    }
}